/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_connect_callback_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleConnectCallbackStub::SleConnectCallbackStub()
{
    memberFuncMap_[SleConnectCallbackInterfaceCode::SLE_CALLBACK_CONNECT_STATE] =
        &SleConnectCallbackStub::OnConnectStateChangeInner;
    memberFuncMap_[SleConnectCallbackInterfaceCode::SLE_CALLBACK_CONNECT_PARAM_UPDATE_COMPLETE] =
        &SleConnectCallbackStub::OnConnectParamUpdateCompleteInner;
    memberFuncMap_[SleConnectCallbackInterfaceCode::SLE_CALLBACK_AUTH_COMPLETE] =
        &SleConnectCallbackStub::OnAuthCompleteInner;
    memberFuncMap_[SleConnectCallbackInterfaceCode::SLE_CALLBACK_PAIR_COMPLETE] =
        &SleConnectCallbackStub::OnPairCompleteInner;
    memberFuncMap_[SleConnectCallbackInterfaceCode::SLE_CALLBACK_READ_RSSI] =
        &SleConnectCallbackStub::OnReadRssiCompleteInner;
}

SleConnectCallbackStub::~SleConnectCallbackStub(){}

int SleConnectCallbackStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    std::u16string descriptor = SleConnectCallbackStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor,
        ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleConnectCallbackInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}


int SleConnectCallbackStub::OnConnectStateChangeInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnConnectStateChangeInner In");
    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connectId id failed!");

    std::shared_ptr<SleAddress> address(data.ReadParcelable<SleAddress>());
    SLE_ASSERT_AND_RETURN_RET_LOG(address, TRANSACTION_ERR, "read address failed!");

    int32_t state;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadInt32(state), TRANSACTION_ERR, "read connect state failed!");

    int32_t pairState;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadInt32(pairState), TRANSACTION_ERR, "read pair state failed!");

    uint16_t reason;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(reason), TRANSACTION_ERR, "read reason failed!");

    OnConnectStateChange(connectId, *address, state, pairState, reason);
    return NO_ERROR;
}

int SleConnectCallbackStub::OnConnectParamUpdateCompleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnConnectParamUpdateCompleteInner In");
    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connectId id failed!");

    uint16_t interval;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(interval), TRANSACTION_ERR, "read interval failed!");

    uint16_t latency;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(latency), TRANSACTION_ERR, "read latency failed!");

    uint16_t timeout;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(timeout), TRANSACTION_ERR, "read timeout failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnConnectParamUpdateComplete(connectId, interval, latency, timeout, errCode);
    return NO_ERROR;
}

int SleConnectCallbackStub::OnAuthCompleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnAuthCompleteInner In");
    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connectId id failed!");

    std::shared_ptr<SleAddress> address(data.ReadParcelable<SleAddress>());
    SLE_ASSERT_AND_RETURN_RET_LOG(address, TRANSACTION_ERR, "read address failed!");

    std::shared_ptr<SleAuthenticationInfo> info(data.ReadParcelable<SleAuthenticationInfo>());
    SLE_ASSERT_AND_RETURN_RET_LOG(info, TRANSACTION_ERR, "read auth info failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnAuthComplete(connectId, *address, *info, errCode);
    return NO_ERROR;
}

int SleConnectCallbackStub::OnPairCompleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnPairCompleteInner In");
    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connectId id failed!");

    std::shared_ptr<SleAddress> address(data.ReadParcelable<SleAddress>());
    SLE_ASSERT_AND_RETURN_RET_LOG(address, TRANSACTION_ERR, "read address failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnPairComplete(connectId, *address, errCode);
    return NO_ERROR;
}

int SleConnectCallbackStub::OnReadRssiCompleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnReadRssiCompleteInner In");
    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connectId id failed!");

    int8_t rssi;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadInt8(rssi), TRANSACTION_ERR, "read rssi failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnReadRssiComplete(connectId, rssi, errCode);
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS